﻿using QD.SQLHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QD.Tools;
using QD_Clients.Models;
using QD_Clients.Code.DbContent;

namespace QD_Clients.Code
{
    public static class PM_ReqsHelper
    {
        /// <summary>
        /// 添加需求处理记录
        /// </summary>
        public static void AddBugProcessList(QDResources db, string RSID, string ProcessorCode, string ProcessorName, string ProcessDesc, string ProcessAction, string NextProcessorCode, string NextProcessorName)
        {
            PM_ReqsProcessList mod = new PM_ReqsProcessList();
            mod.RSID = RSID;
            mod.ProcessorCode = ProcessorCode;
            mod.ProcessorName = ProcessorName;
            mod.ProcessDesc = ProcessDesc;
            mod.ProcessAction = ProcessAction;
            mod.NextProcessorCode = NextProcessorCode;
            mod.NextProcessorName = NextProcessorName;
            mod.ProcessDate = DateTime.Now;


            db.PM_ReqsProcessList.Add(mod);
        }

        /// <summary>
        /// 绑定需求处理日志
        /// </summary>
        /// <param name="listLog"></param>
        /// <param name="rsId"></param>
        public static void BindProcessList(ListView listLog, string rsId)
        {
            listLog.Items.Clear();
            QDResources dbConfig = new QDResources();

            DataTable dt = dbConfig.Query("select * from PM_ReqsProcessList where RSID='" + rsId + "' order by ProcessDate desc");
            int i = 1;
            foreach (DataRow dr in dt.Rows)
            {
                string _logContext = string.Format("[{0}]{1}{2}了当前需求", Convert.ToDateTime(dr["ProcessDate"]).ToString("yyyy-MM-dd HH:mm:ss"), dr["ProcessorName"].ToString(), dr["ProcessAction"].ToString());
                if (!string.IsNullOrEmpty(dr["NextProcessorName"].ToString()))
                {
                    _logContext += "，下步处理人:" + dr["NextProcessorName"].ToString() + "";
                }
                listLog.Items.Add(new ListViewItem(new string[] { i.ToString(), _logContext, dr["ProcessDesc"].ToString() }));
                i++;
            }
        }

        /// <summary>
        /// 初始化待处理需求的DataGridView
        /// </summary>
        /// <param name="grid"></param>
        public static void InitWaitProcessGrid(DataGridView grid)
        {
            grid.InitGrid(false, false, true);
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            grid.CellDoubleClick += grid_CellDoubleClick;
        }

        static void grid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                string _rsid = ((DataGridView)sender).Rows[e.RowIndex].Cells["listRSID"].Value.ToString();

                //Form moFrm = Application.OpenForms["Reqs_Process"];
                //if (moFrm == null)
                //{
                //    moFrm = new Reqs_Process();
                //}
                //moFrm.Show();
                //((Reqs_Process)moFrm).InitUIData(_rsid);
            }
        }

        /// <summary>
        /// 绑定待处理的需求
        /// </summary>
        public static void BindWaitProcessReqs(GroupBox bx, DataGridView grid,string qfcode)
        {
            QDResources dbConfig = new QDResources();
            string sql_reqs = "";
            if (QDUserHelper.CurUser.IsManager)
            {
                sql_reqs = "select  top 100 RSID,'['+convert(varchar(16),isnull(LastProcessDate,CreateDate), 120)+' '+SubmitToName+'] '+ReqtName as ReqtName from PM_Reqs where  status='waitingProcess' and  QfCode='" + qfcode + "' order by LastProcessDate desc";
            }
            else
            {
                sql_reqs = string.Format("select  top 100 RSID,'['+convert(varchar(16),isnull(LastProcessDate,CreateDate), 120)+' '+SubmitToName+'] '+ReqtName as ReqtName from PM_Reqs where NextProcessorCode='{0}' and status='waitingProcess' and  QfCode='" + qfcode + "' order by LastProcessDate desc", QDUserHelper.CurUser.Uid);
            }
            DataTable dt_reqs = dbConfig.Query(sql_reqs);
            grid.DataSource = dt_reqs;
            if (bx != null)
            {
                bx.Text = "待处理需求(" + dt_reqs.Rows.Count.ToString() + ")";
            }
        }

        /// <summary>
        /// 返回一个新的需求编号
        /// </summary>
        /// <returns></returns>
        public static string GetNewId()
        {
            QDResources db = new QDResources();
            object tsidObj = db.ExecuteScalar("select max(RSID) from PM_Reqs");
            if (tsidObj == null || tsidObj.ToString() == "")
            {
                return "RS00001";
            }
            else
            {
                string id = tsidObj.ToString().Substring(tsidObj.ToString().Length - 5, 5);
                int _maxId = Convert.ToInt32(id);
                id = tsidObj.ToString().Substring(0, tsidObj.ToString().Length - id.Length);
                id = id + (_maxId + 1).ToString().PadLeft(5, '0');
                return id;
                //int _maxId = Convert.ToInt32(tsidObj.ToString().Replace("RS", "").TrimStart('0'));
                //return "RS" + (_maxId + 1).ToString().PadLeft(5, '0');
            }
        }
    }
}